package com.itsccn.quartzdemo.scheduler;

import java.util.Date;
import javax.annotation.PostConstruct;

import static org.quartz.CronScheduleBuilder.cronSchedule;  
import org.apache.log4j.Logger;
import org.quartz.CronTrigger;
import org.quartz.DateBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.SchedulerMetaData;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.stereotype.Component;

import com.itsccn.quartzdemo.Job.HelloJob;

import org.apache.log4j.spi.LoggerFactory;
import org.quartz.SimpleTrigger;

public class JobStateExample {
	public void run() throws Exception {  
        Logger log = Logger.getLogger(JobStateExample.class);  
  
        log.info("------- Initializing -------------------");  
  
        // 得到调度器的引用  
        SchedulerFactory sf = new StdSchedulerFactory();  
        Scheduler sched = sf.getScheduler();  
  
        log.info("------- Initialization Complete --------");  
  
        log.info("------- Scheduling Jobs ----------------");  
  
        // 开始时间秒数是10的倍数  
        Date startTime =DateBuilder.nextGivenSecondDate(null, 10);  
  
        // job1 会在秒数是10的倍数开始，重复4次，每10秒重复1次  
        JobDetail job1 =JobBuilder.newJob(HelloJob.class).withIdentity("job1", "group1")  
                .build();  
        SimpleTrigger trigger1 =TriggerBuilder.newTrigger()  
                .withIdentity("trigger1", "group1").startAt(startTime)  
                .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(10)  
                                .withRepeatCount(4)).build();  
        // 向 job1 传递参数  
        job1.getJobDataMap().put(HelloJob.FAVORITE_COLOR, "Green");  
        job1.getJobDataMap().put(HelloJob.EXECUTION_COUNT, 1);  
        Date scheduleTime1 = sched.scheduleJob(job1, trigger1);  
        log.info(job1.getKey() + " will run at: " + scheduleTime1  
                + " and repeat: " + trigger1.getRepeatCount()  
                + " times, every " + trigger1.getRepeatInterval() / 1000  
                + " seconds");  
  
        // job2 会在秒数是10的倍数开始，重复4次，每10秒重复1次  
        JobDetail job2 = JobBuilder.newJob(HelloJob.class).withIdentity("job2", "group1")  
                .build();  
        SimpleTrigger trigger2 = TriggerBuilder.newTrigger()  
                .withIdentity("trigger2", "group1").startAt(startTime)  
                .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(10)  
                                .withRepeatCount(4)).build();  
        // 向 job2传递参数  
        job2.getJobDataMap().put(HelloJob.FAVORITE_COLOR, "Red");  
        job2.getJobDataMap().put(HelloJob.EXECUTION_COUNT, 1);  
        Date scheduleTime2 = sched.scheduleJob(job2, trigger2);  
        log.info(job2.getKey().toString() + " will run at: " + scheduleTime2  
                + " and repeat: " + trigger2.getRepeatCount()  
                + " times, every " + trigger2.getRepeatInterval() / 1000  
                + " seconds");  
  
        log.info("------- Starting Scheduler ----------------");  
  
        // 所有的任务已加入调度器，但调度器未开始之前所有的任务都不会运行  
        sched.start();  
  
        log.info("------- Started Scheduler -----------------");  
  
        log.info("------- Waiting 60 seconds... -------------");  
        try {  
            Thread.sleep(60L * 1000L);  
        } catch (Exception e) {  
            //  
        }  
  
        log.info("------- Shutting Down ---------------------");  
  
        sched.shutdown(true);  
  
        log.info("------- Shutdown Complete -----------------");  
  
        SchedulerMetaData metaData = sched.getMetaData();  
        log.info("Executed " + metaData.getNumberOfJobsExecuted() + " jobs.");  
  
    }  
  
    public static void main(String[] args) throws Exception {  
  
        JobStateExample example = new JobStateExample();  
        example.run();  
    }  
}
